package com.platform.api;

import com.google.common.collect.Maps;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.oss.OSSFactory;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.RRException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "退货管理")
@RestController
@RequestMapping("/api/return")
public class ApiReturnController extends ApiBaseAction {
    @Autowired
    private ApiOrderGoodsService apiOrderGoodsService;

    @Autowired
    private ApiGoodsService apiGoodsService;

    @Autowired
    private ApiReturnService apiReturnService;

    @Autowired
    private ApiOrderService apiOrderService;

    @Autowired
    private ApiMacroService apiMacroService;

    @ApiOperation(value = "申请退货详情")
    @PostMapping("/apply")
    public Object apply(@LoginUser UserVo loginUser, Integer orderGoodsId) {
        if (orderGoodsId == null){
            return toResponsFail("当前订单不存在");
        }
        OrderGoodsVo orderGoodsVo = apiOrderGoodsService.queryObject(orderGoodsId);
        if (orderGoodsVo == null){
            return toResponsFail("当前订单不存在");
        }

        GoodsVo goodsVo = apiGoodsService.queryObject(orderGoodsVo.getGoods_id());

        Map result = Maps.newHashMap();
        result.put("goods_name", orderGoodsVo.getGoods_name());
        result.put("number", orderGoodsVo.getNumber());
        result.put("list_pic_url", orderGoodsVo.getList_pic_url());
        result.put("goods_specifition_name_value", orderGoodsVo.getGoods_specifition_name_value());
        result.put("actual_price", orderGoodsVo.getRetail_price().subtract(orderGoodsVo.getActual_discount_price()));
        result.put("return_description", goodsVo.getReturn_description());
        return toResponsSuccess(result);

    }


    @ApiOperation(value = "退货原因")
    @PostMapping("/returnReason")
    public Object returnReason() throws Exception {
        return toResponsSuccess(apiMacroService.getByKey("order_goods_return_reason"));
    }


    @ApiOperation(value = "退货费用")
    @PostMapping("/returnMoney")
    public Object returnMoney(Integer orderGoodsId, Integer number) throws Exception {
        if (orderGoodsId == null){
            return toResponsFail("当前订单不存在");
        }

        if (number == null || number == 0){
            return toResponsFail("请选择退货数量");
        }

        OrderGoodsVo orderGoodsVo = apiOrderGoodsService.queryObject(orderGoodsId);
        if (number > orderGoodsVo.getNumber()){
            return toResponsFail("退货数量大于购买数量");
        }

        OrderVo orderVo = apiOrderService.queryObject(orderGoodsVo.getOrder_id());
        BigDecimal price = BigDecimal.ZERO;
        if (orderVo.getCoupon_id() == null){
            //没有使用优惠券
            price = orderGoodsVo.getRetail_price().subtract(orderGoodsVo.getActual_discount_price()).multiply(new BigDecimal(number));
        } else {
            //使用优惠券
            price = orderGoodsVo.getRetail_price().subtract(orderGoodsVo.getActual_discount_price()).multiply(new BigDecimal(number)).subtract(orderVo.getCoupon_price());
        }
        return toResponsSuccess(price);
    }



    @ApiOperation(value = "申请退货提交")
    @PostMapping("/submit")
    public Object submit() {
        R r = apiReturnService.submit(getJsonRequest());
        if (r.isOK()){
            return toResponsSuccess("退货申请成功");
        } else {
            return toResponsFail(r.errorMessage());
        }

    }

    @ApiOperation(value = "退货申请上传照片")
    @PostMapping("/uploadPhoto")
    public Object uploadPhoto(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        String url = OSSFactory.build().upload(file);

        R r = new R();
        r.put("url", url);
        r.put("link", url);
        return r;
    }



}